package FileAIS;
our $VERSION=1.00;
use strict;
use warnings;

########### File Open
sub Open2Hash {
# input
	my ($file,$key) = @_;
# tmp
	my %key_index;	my $head_hash;
# output
	my $result_hash;
	my %hash;


	# 解析关键 字段
	my @key = split(/:/,$key);

	open IN,"$file" or die;
	while (<IN>) {
		chomp;
		my $line = $_;
		##### head
		if (/^#/) {
			$line =~ s/^#//;
			$head_hash = &Get_head($line);
			# sub
			foreach my $key (@key) {
				$key_index{$key} = $head_hash->{$key};
			}

			next;
		}
		##### info
		my @tmp = split(/\t/,$line);
# key 
		my @tmp_key; my $key_o;
		foreach my $key (@key) {
			my $index = $key_index{$key};
			my $i = $tmp[$index];
			push @tmp_key,$i;
		}
		$key_o = join(':',@tmp_key);
# info
		foreach my $head (keys %{$head_hash}) {
			my $index = $head_hash->{$head};
			$hash{$key_o}{$head} = $tmp[$index];
		}
	}
	close IN;

	$result_hash = \%hash;
	return $result_hash;
}
sub Get_head {
	my $line = shift;
	my %hash;
	my $result;

	my @tmp = split(/\t/,$line);
	for (my $i=0;$i<=$#tmp;$i++) {
		$hash{$tmp[$i]} = $i;
	}

	$result = \%hash;
	return $result;
}
########### File Open

########### File Output
sub Hash2File {
	my ($ref_hash,$file) = @_;
	my @head;

	# head
	foreach my $id (sort keys %{$ref_hash}) {
		@head = sort keys %{$ref_hash->{$id}};
		last;
	}

	open O,">$file" or die;
	# head
	print O "#";
	print O join("\t",@head),"\n";
	# info
	foreach my $id (sort keys %{$ref_hash}) {
		my $line;
		foreach my $head (@head) {
			my $value = $ref_hash->{$id}->{$head};
			$value = 'Null' if (! defined $value);
			$value =~ s/\n+//g;
			$value =~ s/\t+/ /g;
			$line .= $value."\t";
		}
		$line =~ s/\t$//;
		print O "$line\n";
	}
	close O;
}
########### File Output

########### File import DB
sub File2dbIU {
	my ($file,$table,$key) = @_;
	my $condition; my $sub;

	my $head_hash; my %sub_index; my %value;
	# parse key
	my @key = split(/:/,$key);

	open IN,"$file" or die;
	while (<IN>) {
		chomp;
		my $line = $_;
		#### head
		if (/^#/) {
			$line =~ s/^#//;
			$head_hash = &Get_head($line);
			# sub
			foreach my $i (@key) {
				$sub_index{$i} = $head_hash->{$i};
			}

			next;
		}
		#### into DB
		my @tmp = split(/\t/,$line);
		# value
		%value=();
		foreach my $i (@key) {
			my $index = $sub_index{$i};
			$value{$i} = $tmp[$index];
		}
		# data
		my %data;
		foreach my $fields (keys %{$head_hash}) {
			my $index = $head_hash->{$fields};
			$data{$fields} = $tmp[$index];
		}
			# confition
		my $condition;
		foreach my $i (@key) {
			my $value = $value{$i};
			$condition .= "$i = '$value' and ";
		}
		$condition =~ s/and $//g;
#		print "$condition\n";
		&MySql::InsertOrUpdate($table,\%data,$condition);
	}
	close IN;
}
sub File2dbI {
	my ($file,$table) = @_;
	my $head_hash;

	open IN,"$file" or die;
	while (<IN>) {
		chomp;
		my $line = $_;
		#### head
		if (/^#/) {
			$line =~ s/^#//;
			$head_hash = &Get_head($line);

			next;
		}
		#### into DB
		my @tmp = split(/\t/,$line);

		my %data = ();
		foreach my $fields (keys %{$head_hash}) {
			my $index = $head_hash->{$fields};
			$data{$fields} = $tmp[$index];
		}

		&MySql::insert($table,\%data);
	}
	close IN;
}
########### File import DB

###########  DB to HASH
sub DB2Hash {
	my ($table,$key,$cdt,$list_array) = @_;
# tmp
	my $num = scalar(@{$list_array});
	my $list = '*';
# output
	my %hash = ();my $result;
	
	if ($num > 0) {
	# condition
		my $condition;
		foreach my $i (@{$list_array}) {
			$condition .= "$cdt = '$i' or ";
		}
		$condition =~ s/or $//;

		$result = &MySql::view_table($table,$condition,$key,$list);
	} else {
		$result = \%hash;
	}
	return $result;
}
